package com.vipheyue.campusrecyce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.vipheyue.campusrecyce.R;
import com.vipheyue.campusrecyce.activity.BrowseActivity;
import com.vipheyue.campusrecyce.adapter.BaseRecyclerAdapter;
import com.vipheyue.campusrecyce.adapter.BaseRecyclerViewHolder;
import com.vipheyue.campusrecyce.bean.CategoryBean;
import com.vipheyue.campusrecyce.utils.CheckTextUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReNameFragment0 extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.et_fragment0_searchName)
    EditText et_fragment0_searchName;
    private List<CategoryBean> mDatas = new ArrayList<CategoryBean>();
    private int nowPageNum = 0;
    private int pageSize = 50;
    private BaseRecyclerAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_re_name_fragment0, container, false);
        ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    private void initData() {
        //        String[] type = {"校园代步","手机","电脑","数码配件","数码","电器","运动健身","衣物伞帽","图书教材","租赁","生活娱乐","其他"};
        mDatas.add(new CategoryBean(R.drawable.type_1, "百货副食"));
        mDatas.add(new CategoryBean(R.drawable.type_2, "电脑办公"));
        mDatas.add(new CategoryBean(R.drawable.type_3, "化妆个护"));
        mDatas.add(new CategoryBean(R.drawable.type_4, "计生保健"));
        mDatas.add(new CategoryBean(R.drawable.type_5, "旅行交友"));
        mDatas.add(new CategoryBean(R.drawable.type_6, "生活娱乐"));
        mDatas.add(new CategoryBean(R.drawable.type_7, "手机数码"));
        mDatas.add(new CategoryBean(R.drawable.type_8, "图书教材"));
        mDatas.add(new CategoryBean(R.drawable.type_9, "文房乐器"));
        mDatas.add(new CategoryBean(R.drawable.type_10, "箱包服饰"));
        mDatas.add(new CategoryBean(R.drawable.type_11, "校园代步"));
        mDatas.add(new CategoryBean(R.drawable.type_12, "运动健身"));
    }

    private void initView() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(layoutManager);//设置显示方式

//        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())//第三
//                .drawable(R.drawable.divider)//这里可以是图片
//                .size(10)
//                .build());


//        swipe_target.setAdapter(adapter);//设置适配器
        adapter = new BaseRecyclerAdapter<CategoryBean>(getActivity(), mDatas, R.layout.category_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, CategoryBean bean, int position) {
                holder.setText(R.id.tv_name, bean.getName());//TODO 这里因该是schoolName
                holder.setCircleImage(getActivity(), R.id.iv_category, bean.getImgId());//TODO 设置一个 默认的 本来和下面一句 是else的

            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
                CategoryBean bean = mDatas.get(position);
                Intent intent = new Intent(getActivity(), BrowseActivity.class);
                intent.putExtra("Category", bean.getName());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);//设置适配器
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_fragment0_search)
    public void onClick() {
        if (CheckTextUtil.textIsEmpty(et_fragment0_searchName)) {
            Toast.makeText(getActivity(), "请输入要搜索的东西", Toast.LENGTH_SHORT).show();
        }else{
            Intent intent3 = new Intent(getActivity(), BrowseActivity.class);
            intent3.putExtra("searchName", et_fragment0_searchName.getText().toString());
            startActivity(intent3);
        }
    }
}
